
import com.itheima.domain.store.Question;
import com.itheima.service.store.QuestionService;
import com.itheima.service.store.impl.QuestionServiceImpl;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Program: heimamm
 * @Description:
 * @Author: choulan
 * @Date:2020-10-22 14:04
 **/
public class Dss {
    static Map<String, Object> getTitles(Class<?> clazz) {
        List<Field> newFields = new ArrayList<>();
        List<String> titles = new ArrayList<>();

        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field f : declaredFields) {
            TdTitle annotation = f.getAnnotation(TdTitle.class);
            if (annotation == null) {
                continue;
            }
            newFields.add(f);
            titles.add(annotation.title());
        }

        Map<String, Object> map = new HashMap<>();
        map.put("title", titles);
        map.put("field", newFields);
        return map;
    }

    static void applysheet(Sheet sheet, List<?> list, int firstRow, int firstCol) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        if(list!=null&&list.size()!=0){
            map=getTitles(list.get(0).getClass());
        }
        //获得title
        List<String> titles=(List<String>)map.get("title");
        Row row = sheet.createRow(firstRow );
        for (int i = 0; i < titles.size(); i++) {
            //起一个标题行
            Cell cell = row.createCell(firstCol + i);
            //为该行的每一列赋值
            cell.setCellValue(titles.get(i));
        }
        //获得列数据
        List<Field> newFields =(List<Field>) map.get("field");
        for (int i = 0; i < list.size(); i++) {
            Object key = list.get(i);
            //在标题行下重起一行
            Row row1=sheet.createRow(firstRow+i+1);
            for (int j = 0; j < newFields.size(); j++) {
                //在新起的一行写列数据
                Cell cell = row1.createCell(firstCol + j);
                //获得属性
                Field field = newFields.get(j);
                field.setAccessible(true);
                //获得属性的值
                Object value = field.get(key);
                //为列赋值
                cell.setCellValue(value.toString());
            }
        }

    }
    @Test
    public void  test01() throws IOException, IllegalAccessException {
        List<Question> list=new ArrayList<>();
        Question q1=new Question();
        q1.setId("101");
        q1.setCatalogId("cata205");
        q1.setCompanyId("cop101");
        q1.setRemark("大流口水的将来");
        list.add(q1);


        Question q2=new Question();
        q2.setId("101");
        q2.setCatalogId("cata205");
        q2.setCompanyId("cop101");
        q2.setRemark("");
        list.add(q2);
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("题目数据文件");

        applysheet(sheet,list,1,1);

        //将内存中的数据写到本地excel表中
        File f=new File("test1.xlsx");
        OutputStream os=new FileOutputStream(f);
        workbook.write(os);
        workbook.close();
        os.close();
    }
}
